﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using Job.Web.Framework;
using Job.Logic.Personal;
using Job.Entity;

namespace Job.Web.Person
{
    public partial class PersonRegister : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
               // this.btCreate.Attributes.Add("onclick", "return enableSubmit();");
            }
            this.msg.Visible = false;
            this.lbErrMsg.Text = "";
        }

        protected void btCreate_Click(object sender, EventArgs e)
        {
            try
            {
                if (!this.chkAllow.Checked)
                {
                    this.msg.Visible = true;
                    this.lbErrMsg.Text = "只有同意服务条款后，才能注册帐户！";
                    return;
                }
                

                PersonalLogic logic = new PersonalLogic();
                string loginCode = tbLogin.Text.Trim();

                JobPersonAccount account = logic.Login(loginCode);
                if (null != account)
                {
                    this.msg.Visible = true;
                    this.lbErrMsg.Text = "账号(" + loginCode + "已被其它用户注册，请重新指定登录名！";
                    return;
                }

                account = new JobPersonAccount();
                account.LoginName = tbLogin.Text.Trim();
                account.Password = tbPassword1.Text;
                account.UserName = tbName.Text.Trim();
                account.UserSex = Convert.ToInt32(rblSex.SelectedValue);
                account.UserCard = tbCardId.Text.Trim();
                account.LinkEmail = tbEmail.Text.Trim();

                logic.CreateAccount(account);

                account = logic.Login(account.LoginName);
                if (null == account)
                {
                    this.msg.Visible = true;
                    this.lbErrMsg.Text = "系统内部故障，保存帐号失败！";
                }
                else
                {
                    SessionPersonal spersonal = new SessionPersonal()
                    {
                        LoginName = account.LoginName,
                        PersonAccountid = account.PersonAccountId,
                        UserName = account.UserName
                    };

                    MySession session = new MySession();
                    session.AddPersonal2Session(spersonal);

                    string url = WebCommon.GetApplicationPath(Request) + "Person/PersonManager.aspx?categoryid=1&ctrlId="
                        + ((int)Constants.ResumeManagerType.CreateResume).ToString();
                    Response.Redirect(url);
                }

            }
            catch (Exception ex)
            {
                this.msg.Visible = true;
                this.lbErrMsg.Text = "系统异常，无法创建用户！";
            }
        }

    }
}
